package thinker;

import chess.*;

public class SearchTree {

	Node root;

	public SearchTree(Node r) {
		root = r;
	}

}

class Node {

	public static final int MAX = Integer.MAX_VALUE;
	public static final int MIN = Integer.MIN_VALUE;
	public static byte myColor;
	public static int count = 0;

	ChessBoard board;
	Chess lastMove;
	byte turn;
	int worth;

	Node farther;
	Node children;
	Node nextNode;
	Node tail;
	int depth;

	public Node() {
		board = null;
		lastMove = null;
		turn = 0;
		worth = 0;
		farther = null;
		children = null;
		nextNode = null;
		tail = null;
		depth = 0;
		count++;
	}

	public Node(ChessBoard b) {
		board = new ChessBoard(b);
		lastMove = null;
		turn = b.turn;
		worth = 0;
		farther = null;
		children = null;
		nextNode = null;
		tail = null;
		depth = 0;
		count++;
	}

	public void addChild(Chess c) {
		Node n = new Node();
		n.lastMove = c;
		n.turn = (byte) ((turn + 1) % 2);
		n.farther = this;
		if (children == null) {
			children = n;
		}
		else {
			tail.nextNode = n;
		}
		tail = n;
		n.depth = depth + 1;
	}

	public ChessBoard getBoard() {
		if (depth == 0) {
			return new ChessBoard(board);
		}
		else {
			ChessBoard b = farther.getBoard();
			b.update(lastMove);
			return b;
		}
	}

	public boolean isMaxNode() {
		return (turn == myColor);
	}

	public boolean isMinNode() {
		return (turn != myColor);
	}

}
